import { finance } from "../../../../js/modules/store.js";
import {config} from "../../../../js/modules/gloab";
import {router} from '../../../../js/router';
import { forms, storage, util, api } from '../../../../js/util';
import authorize from '../../../../js/modules/authorize.js'
import { theme } from '../../../../js/modules/theme'
const app = getApp()
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"recharge",

  /**
   * 页面的初始数据
   */
  data: {
    prestore:{},
    fixRewards: [],
    reward:0,
    rewards:[],
    recharge: 0,
    rateDisplay: false,
    rechargeDisplay: true,
    comfireClass: "disable",
    rechargeAmount: "",
    isRecharge:false,
    finalrecharge:0,
    Withdrawals:true,
    ProprietorVersion:0,
    mainColor: '', // 主题色
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.setData({
      mainColor: api.titleColor || "#e56b14"
    })
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    var entity = authorize.getEntity() || authorize.getDefault();
    config.getAuthorityConfig(
      fkflag,
      fkid,
      entity.manager.Token,
      function(res){
        if(res.Success){
          let data = res.Content;
          //小程序版本ProprietorVersion==1006时不显示申请提现
          that.setData({
            ProprietorVersion: data.ProprietorVersion
          });
        }
      },
      true
    )
    config.getWholesaleConfigs(
      fkid,
      fkflag,
      "Wholesale",
      (model) => {
        that.setData({
          isRecharge: model.IsRecharge,
          Withdrawals:model.Withdrawals
        });
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    //加载预存款信息
    finance.get_prestore_info(function(res){
      res=res.data;
      if(res.Success){
        that.setData({
          prestore: res.Content
        });
      }
       
    });
    //充值配置
    finance.get_recharge_reward_config(function (rewards,fixRewards, rateRewards) {
   
      that.setData({
        fixRewards: fixRewards,
        rateDisplay: rateRewards.length == 0,
        rateRewards: rateRewards,
        rewards: rewards
      });
    });
  },
  /**
   * 充值框事件
   */
  rechargeInputEvent: function (e) {
    var v = e.detail.value;
    var reg = forms.validateRegExp["price"];
    var aa = reg.test(v);               
    //为空时，控制不能点
    if(v==""){
      this.setData({
        isCaculate:false,
        comfireClass: "disable"
      });
      return; 
    }
    //输入错误时,且输入不能大于100w
    if (!reg.test(v) || parseFloat(v) > 10000000) {
      this.setData({
        rechargeAmount: 0,
        isCaculate:false,
        comfireClass: "disable"
      });
      return;
    };
    this.setData({
      rechargeAmount: v,
      isCaculate:false,
      comfireClass: ""
    });
    this.onCaculateReward();
  },
  /**
   * 计算奖励
   *  */
  onCaculateReward: function () {
    var that = this;
    if (that.data.comfireClass == "disable") return;

    var finalrecharge = parseFloat(that.data.rechargeAmount);//充值金额
    var info = finance.caculate_reward(finalrecharge, that.data.rewards, 'input');
    if(info==null) return;
    this.setData({
      finalrecharge: info.finalrecharge,
      rewardDesc: info.rewardDesc,
      reward: info.reward,
      isCaculate: true,
      comfireClass: "disable"
    });
    
  },
  goDetail() {
    theme.routerTo("rechargeDetail");
  },
  /**
   * 充值
   *  */
  onRechargeEvent: function () {
    var that = this;
    if (!that.data.isCaculate) {
      util.alert("请先计算奖励金额");
      return;
    }
    if (that.data.rechargeAmount <= 0) {
      util.alert("请选择(或输入)需充值的金额!");
      return;
    }
    finance.goRecharge(that.data.rechargeAmount, that.data.reward);
  },
 
  /**
   * 选择充值金额
   */
  onSwitchEvent: function (e) {
    var edata = e.currentTarget.dataset

    var recharge = parseFloat(edata.amount);
    var info = finance.caculate_reward(recharge, this.data.rewards, 'select');
    if (info == null) return;

    this.setData({
      recharge: recharge,
      rechargeAmount: recharge,
      reward: info.reward,
      isCaculate: true,
      finalrecharge: info.finalrecharge,
      rewardDesc: info.rewardDesc
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  },
  /**
   * 提现
   */
  goCashOut: function () {
    router.navigate({
      path: 'distributionCashOut',
      params: {
        type: 2 //预存款提现
      }
    })
  },
})